package org.lanqiao.tjut.db;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.datasource.DataSourceFactory;

public class DBCPDataSourceFactory implements DataSourceFactory {
	private BasicDataSource datasource = null;

	public DBCPDataSourceFactory() {
		this.datasource = new BasicDataSource();
	}

	@Override
	public DataSource getDataSource() {
		return datasource;
	}

	@Override
	public void setProperties(Properties ps) {
		datasource.setDriverClassName(ps.getProperty("driver"));
		datasource.setUsername(ps.getProperty("username"));
		datasource.setUrl(ps.getProperty("url"));
		datasource.setPassword(ps.getProperty("password"));
		
		// dbcp连接池属性设置
		datasource.setMaxIdle(Integer.parseInt(ps.getProperty("maxIdle", "20")));
		datasource.setMinIdle(Integer.parseInt(ps.getProperty("minIdle", "2")));
		datasource.setInitialSize(Integer.parseInt(ps.getProperty("initialSize", "3")));
		datasource.setLogAbandoned(Boolean.parseBoolean(ps.getProperty("logAbandoned", "true")));
		datasource.setRemoveAbandonedTimeout(Integer.parseInt(ps.getProperty("removeAbandonedTimeout", "10")));
		datasource.setMaxWaitMillis(Integer.parseInt(ps.getProperty("maxWait", "10000")));
		datasource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(ps.getProperty("timeBetweenEvictionRunsMillis", "10000")));
		datasource.setNumTestsPerEvictionRun(Integer.parseInt(ps.getProperty("numTestsPerEvictionRun", "10")));
		datasource.setMinEvictableIdleTimeMillis(Integer.parseInt(ps.getProperty("minEvictableIdleTimeMillis", "10000")));

		/*
		 * maxActive=150 minIdle=2 maxIdle=20 initialSize=3 logAbandoned=true
		 * removeAbandoned=true removeAbandonedTimeout=10 maxWait=1000
		 * timeBetweenEvictionRunsMillis=10000 numTestsPerEvictionRun=10
		 * minEvictableIdleTimeMillis=10000
		 */
	}

}
